#!/bin/sh

#Java安装目录
#JAVA_HOME=
#程序启动参数
#JAVA_OPTS=

#应用程序名称
APP_NAME="SAppServer"
APP_LONG_NAME="Simple Application Server"

#程序启动主类
MAIN_CLASS="com.freecsim.sas.kernel.Kernel"
STOP_CLASS="com.freecsim.sas.Stop"

#java 启动命令
_RUNJAVA=java

# Resolve the location of the 'ps' command
PSEXE="/usr/ucb/ps"
    if [ ! -x "$PSEXE" ]
    then
        PSEXE="/usr/bin/ps"
        if [ ! -x "$PSEXE" ]
        then
            PSEXE="/bin/ps"
            if [ ! -x "$PSEXE" ]
            then
                echo "Unable to locate 'ps'."
                echo "Please report this message along with the location of the command on your system."
                exit 1
            fi
        fi
    fi

# Resolve the os
DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
case "$DIST_OS" in
    'sunos')
        DIST_OS="solaris"
        ;;
    'hp-ux' | 'hp-ux64')
        # HP-UX needs the XPG4 version of ps (for -o args)
        DIST_OS="hpux"
        UNIX95=""
        export UNIX95   
        ;;
    'darwin')
        DIST_OS="macosx"
        ;;
    'unix_sv')
        DIST_OS="unixware"
        ;;
    'FreeBSD')
        DIST_OS="freebsd"
        ;;
esac

getpid() {
    pid=""
    if [ -f "$PIDFILE" ]
    then
        if [ -r "$PIDFILE" ]
        then
            pid=`cat "$PIDFILE"`
            if [ "X$pid" != "X" ]
            then
                case "$DIST_OS" in
                    'freebsd')
                        pidtest=`$PSEXE -p $pid -o args | tail -1`
                        if [ "X$pidtest" = "XCOMMAND" ]
                        then 
                            pidtest=""
                        fi
                        ;;
                    'macosx')
                        pidtest=`$PSEXE -ww -p $pid -o command | grep "$_RUNJAVA" | tail -1`
                        ;;
                    'solaris')
                        pidtest=`$PSEXE -auxww  $pid | grep "$_RUNJAVA" | tail -1`
                        ;;                    
                    'hpux')
                        pidtest=`$PSEXE -p $pid -x -o args | grep "$_RUNJAVA" | tail -1`
                        ;;
                    *)
                        pidtest=`$PSEXE -p $pid -o args | grep "$_RUNJAVA" | tail -1`
                        ;;
                esac

                if [ "X$pidtest" = "X" ]
                then
                    # This is a stale pid file.
                    rm -f "$PIDFILE"
                    echo "Removed stale pid file: $PIDFILE"
                    pid=""
                fi
            fi
        else
            echo "Cannot read $PIDFILE."
            exit 1
        fi
    fi
}

#设置JVM运行环境参数
setenv(){
	if [ -n $SAS_HOME ]; then
		SAS_HOME=$(cd `dirname $0`; pwd)
	fi

	#进程ID文件目录
	PIDFILE="$SAS_HOME/$APP_NAME.pid"

	if [ -r "$SAS_HOME/setenv.sh" ]; then
            .  "$SAS_HOME/setenv.sh"
        fi
        if [ ! -n "$JAVA_OPTS" ]; then
           JAVA_OPTS="-server -Xms128m -Xmx128m"
        fi

	#程序包路径
	libs=$SAS_HOME/lib
	
	echo "SAS_HOME: $SAS_HOME"
	echo "JAVA_HOME: $JAVA_HOME"
}

#初始化CLASSPATH
initclspath(){
	for jarpath in $1/*
	do
	   if [ -d $jarpath ]
           then
                initclspath $jarpath
           else
	        temp=$temp:$jarpath
           fi     
	done 
}

start(){
    setenv
    echo "Starting $APP_LONG_NAME..."
    echo "JAVA_HOME: $JAVA_HOME"
    getpid
    if [ "X$pid" = "X" ]
    then
	    temp=$CLASSPATH
		initclspath $libs
	exec $_RUNJAVA $JAVA_OPTS -classpath $temp -Dapp.home=$SAS_HOME $MAIN_CLASS 'nout' > $HOME/logs/console.log & echo $! > $SAS_HOME/$APP_NAME.pid &
    else
        echo "$APP_LONG_NAME is already running."
        exit 0
    fi
}

console(){
    setenv
    echo "Running $APP_LONG_NAME..."
    echo "JAVA_HOME: $JAVA_HOME"
    getpid
    if [ "X$pid" = "X" ]
    then
    	temp=$CLASSPATH
		initclspath $libs
        $_RUNJAVA $JAVA_OPTS -classpath $temp -Dapp.home=$SAS_HOME $MAIN_CLASS
    else
        echo "$APP_LONG_NAME is already running."
        exit 0
    fi    
}

stopit(){
   setenv
   echo "Stoping $APP_LONG_NAME..."
   getpid
   if [ "X$pid" = "X" ]
   then
   	echo "$APP_LONG_NAME is not running."
	exit 0
   else
        case "$1" in
            "-f")
		kill -15 $pid
                ;;
            "--force")
	        kill -15 $pid
                ;;
            *)
           temp=$CLASSPATH
		       initclspath $libs
   	       exec $_RUNJAVA -classpath $temp -Dapp.home=$SAS_HOME $STOP_CLASS $1 &
	       ;;
        esac
        rm -f "$PIDFILE"
   fi
}

status(){
   getpid
   if [ "X$pid" = "X" ]
   then
   	echo "$APP_LONG_NAME is not running."
   else
	echo "$APP_LONG_NAME is running as pid($pid)."
   fi
   exit 1
}

showHelp(){
   echo "Usage: $0 [COMMAND] [OPTION]"
   echo "COMMAND:"
   echo "  start                Start the application in background."
   echo "  stop                 Stop the application."
   echo "  restart              Restart the application."
   echo "  status               Print status of the application."
   echo "  console              Start the application in colsole."
   echo "  help                 Print the status of the application."
   echo " OPTION:"
   echo "   -f, --force         Stop or restart the Application by force"
   exit 1
}

case "$1" in

    'console')
        console
        ;;

    'start')
        start
        ;;

    'stop')
        stopit $2
        ;;

    'restart')
        stopit $2
	sleep 1
        start
        ;;

    'status')
        status
        ;;
    'help')
        showHelp
        ;;
    'h')
        showHelp
        ;;
    *)
        echo "$0: unrecongnized command '$1'" 
        echo "Try '$0 help' for more infomation."
esac